﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using CKS.Core.Framework;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// OP60盖板底座焊丝组合Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IDcOp50RecordService), ServiceLifetime = LifeTime.Transient)]
    public class DcOp50RecordService : BaseService<DcOp50Record>, IDcOp50RecordService
    {
        /// <summary>
        /// 查询OP60盖板底座焊丝组合列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<DcOp50RecordDto> GetList(DcOp50RecordQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                    .OrderByDescending(a => a.Id)
                .ToPage<DcOp50Record, DcOp50RecordDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public DcOp50Record GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加OP60盖板底座焊丝组合
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DcOp50Record AddDcOp50Record(DcOp50Record model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改OP60盖板底座焊丝组合
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateDcOp50Record(DcOp50Record model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<DcOp50Record> QueryExp(DcOp50RecordQueryDto parm)
        {
            var predicate = Expressionable.Create<DcOp50Record>();
            predicate.AndIF(parm.BeginTime != DateTime.MinValue && parm.BeginTime != null, u => u.CreatedTime >= parm.BeginTime.ToBeginTime());
            predicate.AndIF(parm.EndTime != DateTime.MinValue && parm.EndTime != null, u => u.CreatedTime <= parm.EndTime.ToEndTime());
            predicate.AndIF(!parm.CoverCode.IsEmpty(), u => u.CoverCode == parm.CoverCode);
            return predicate;
        }
    }
}